﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class uc_GiaoVien : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai báo biên

        GiaoVien_BLL gv_bll = new GiaoVien_BLL();
        TextMessageBox box = new TextMessageBox();
        DataTable dt;

        #endregion

        #region LoadForm
        public uc_GiaoVien()
        {
            InitializeComponent();
        }
        private void uc_GiaoVien_Load(object sender, EventArgs e)
        {
            LoadData();
            
        }

        #endregion

        #region các chức năng
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmGiaoVien frm = new frmGiaoVien();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.FocusedRowHandle > -1)
            {
                frmGiaoVien frm = new frmGiaoVien();
                frm.maDocGia = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaGiaoVien).ToString();
                if (frm.ShowDialog() == DialogResult.OK)
                    LoadData();
            }
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.FocusedRowHandle > -1)
            {
                GiaoVien_Public gv = new GiaoVien_Public();
                string ma = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaGiaoVien).ToString();
                gv.MaDocGia = ma;
                string ten = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle,colHoVaTen).ToString();
                dt = gv_bll.TaoBang(box.sqlCheckExist("DocGia","SoThe",ma));
                if (dt.Rows.Count > 0)
                {
                    XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        //try
                        //{
                            gv_bll.Delete(gv);
                            LoadData();
                        //}
                        //catch (Exception)
                        //{
                        //    XtraMessageBox.Show(box.DeleteFail,box.Caption_Fail,MessageBoxButtons.OK,MessageBoxIcon.Error);
                        //}
                    }
                }
            }
        }
        #endregion

        #region hàm viết thêm

        private void LoadData()
        {
            grd_Data.DataSource = gv_bll.LayDuLieu();
        }

        #endregion

    }
}
